/*
 * @Descripttion: 
 * @version: 
 * @Author: guangyunl
 * @Date: 2024-01-19 22:30:57
 * @LastEditors: guangyunl
 * @LastEditTime: 2024-03-09 23:54:53
 */
/* indent size: 2 */

module.exports = app => {
  const DataTypes = app.Sequelize;

  const Address = app.model.define('address', {
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      comment: "地址id",
    },
    nickName: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "地址姓名",
    },
    mobile: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "地址联系方式",
    },
    provincial: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "地址-省分",
    },
    city: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "地址-城市",
    },
    address: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "地址-详细地址",
    },   
    is_default: {
      type: DataTypes.STRING,
      allowNull: true,
      defaultValue: '0',
      comment: "地址-是否是默认地址【1->默认地址，0->非默认地址】",
    },
    uid: {
      type: DataTypes.INTEGER,
      // allowNull: true,
      // primaryKey: true,
      comment: "地址所属用户id",
      references: {
        model: 'users',
        key: 'id'
      }
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: "创建时间",
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: "更新时间",
    }
  }, {
    tableName: 'address'
  });

  Address.associate = function () {
    //一个地址属于一个用户
    // 关联用户表一个用户可以有多个地址 (外键在的表)
    app.model.Address.belongsTo(app.model.Users, {
      foreignKey: 'uid',//定义联结关系中源模型的 key
      targetKey: 'id'
    });

 
    //一个地址可以拥有多个订单
    app.model.Address.hasMany(app.model.Orders, {
      foreignKey: 'id',//定义联结关系中源模型的 key
      targetKey: 'aid'
    });

  }

  return Address;
};
